package com.example.panpan.productManage.service;

import com.example.common.utils.IMap;
import com.example.panpan.productManage.mapper.TcProductMapper;
import com.example.panpan.productManage.mapper.TcStockRecordMapper;
import com.example.panpan.productManage.model.TcProduct;
import com.example.panpan.productManage.model.TcStockRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class ProductService {
    @Autowired
    private TcProductMapper tcProductMapper;
    @Autowired
    private TcStockRecordMapper tcStockRecordMapper;

    public List<IMap> getProductList(IMap param){
        return tcProductMapper.getProductList(param);
    }

    public int addProduct(IMap param){
        //判重
        TcProduct tcProduct1 = tcProductMapper.selectByPrimaryKey(param.getString("productId"));
        if (tcProduct1 != null){//在用即返回提示，已作废即直接删除
            if (tcProduct1.getState().equals("1")) {
                return 0;
            }else {
                tcProductMapper.deleteByPrimaryKey(param.getString("productId"));
            }
        }
        TcProduct tcProduct = new TcProduct();
        tcProduct.setProductId(param.getString("productId"));
        tcProduct.setProductName(param.getString("productName"));
        tcProduct.setType(param.getString("type"));
        tcProduct.setAuthor(param.getString("author"));
        tcProduct.setPress(param.getString("press"));
        tcProduct.setPrice(param.getString("price"));
        tcProduct.setCreatUser(param.getString("updateUser"));
        tcProduct.setCreatDate(new Date());
        tcProduct.setStock(0);
        tcProduct.setState("1");
        try {
            tcProductMapper.insert(tcProduct);
        }catch (Exception e){
            return 9999;
        }
        return 1;
    }

    public IMap getProductInfo(IMap productId){
        IMap ret = tcProductMapper.getProductList(productId).get(0);
        return ret;
    }

    public int updProduct(IMap param){
        TcProduct tcProduct = new TcProduct();
        tcProduct.setProductId(param.getString("productId"));
        tcProduct.setProductName(param.getString("productName"));
        tcProduct.setType(param.getString("type"));
        tcProduct.setAuthor(param.getString("author"));
        tcProduct.setPress(param.getString("press"));
        tcProduct.setPrice(param.getString("price"));
        try {
            tcProductMapper.updateByPrimaryKeySelective(tcProduct);
        }catch (Exception e){
            return 0;
        }
        return 1;
    }

    public int delProduct(IMap param){
        TcProduct tcProduct = new TcProduct();
        tcProduct.setProductId(param.getString("productId"));
        tcProduct.setState("0");
        try {
            tcProductMapper.updateByPrimaryKeySelective(tcProduct);
        }catch (Exception e){
            return 0;
        }
        return 1;
    }


    public int doProductIn(IMap param){
        TcProduct tcProduct = new TcProduct();
        tcProduct.setProductId(param.getString("productId"));
        tcProduct.setState("1");
        try {
            tcProductMapper.updateByPrimaryKeySelective(tcProduct);
        }catch (Exception e){
            return 0;
        }
        return 1;
    }

    public List<IMap>getStockChangeList(IMap param){
        return tcStockRecordMapper.getStockChangeList(param);
    }

    public int addStock(IMap param){
        TcProduct tcProduct = tcProductMapper.selectByPrimaryKey(param.getString("productId"));
        int stock = tcProduct.getStock();
        int stockChange = Integer.valueOf(param.getString("addCount"));
        tcProduct.setStock(stock + stockChange);
        tcProductMapper.updateByPrimaryKeySelective(tcProduct);
        TcStockRecord tcStockRecord = new TcStockRecord();
        SimpleDateFormat tempDate = new SimpleDateFormat("yyyyMMddHHmmss");
        String datetime = tempDate.format(new Date());
        tcStockRecord.setRecordId("r"+datetime);
        tcStockRecord.setProductId(param.getString("productId"));
        tcStockRecord.setStockChange(Math.abs(stockChange));
        tcStockRecord.setOperateId(param.getString("updateUser"));
        tcStockRecord.setOperateDate(new Date());
        tcStockRecord.setRecordType("2");
        tcStockRecordMapper.insert(tcStockRecord);
        return 1;
    }
}
